<?php

namespace App\Business\BLL;

use Zttp\Zttp;

class QqMap
{
    public function getCityList()
    {
        $path = '/ws/district/v1/list';
        $url = config('qqmap.domain') . $path;
        $data = $this->sign($path, []);

        $response = Zttp::get($url, $data);

        $responseJson = $response->json();

        return $responseJson;

    }

    public function geocoder($lat, $lng)
    {
        $path = '/ws/geocoder/v1';
        $data['location'] = "$lat,$lng";
        $url = config('qqmap.domain') . $path;
        $data = $this->sign($path, $data);

        $response = Zttp::get($url, $data);
        $responseJson = $response->json();
        return $responseJson;

    }

    public function getDistance($from,$to)
    {
        $path = '/ws/distance/v1/matrix';
        $data['mode'] = "driving";
        $data['from'] = $from;
        $data['to'] = $to;
        $url = config('qqmap.domain') . $path;
        $data = $this->sign($path, $data);
        $response = Zttp::get($url, $data);
        $responseJson = $response->json();
        return $responseJson;
    }

    //签名
    private function sign($url, $date)
    {
        if (!is_array($date)) {
            return false;
        }
        $date['key'] = config('qqmap.key');
        \ksort($date);

        $params = urldecode(http_build_query($date));
        $signString = $url . '?' . $params . config('qqmap.secret_key');
        $sig = md5($signString);
        $date['sig'] = $sig;
        return $date;
    }
}
